package imageProcessing.filter;

import imageProcessing.Duplicator;
import imageProcessing.ImageEvent;
import imageProcessing.ImageListener;

import java.awt.image.renderable.ParameterBlock;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;


public class DilateFilter implements ImageListener{
	float[] _matrix = new float[]{
			0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,			
			0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
			0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
			0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
			1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,			
			0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
			0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
			0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
			0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,			
			0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
	};
	
	private ArrayList<ImageListener> listeners;
	
	
	private PlanarImage m_image;
	
	public DilateFilter(){
		listeners = new ArrayList<ImageListener>();
		m_image = null;
	}

	public void notifyListeners() {
		
			ImageEvent ie  = new ImageEvent(this, m_image);
						
			ArrayList<ImageListener> copy = Duplicator.copyListeners(listeners);
			for (ImageListener listener : copy) {
				listener.imageChanged(ie);
			}
	}
		
	public void addImageListener(ImageListener il){
		listeners.add(il);
	}
	
	public void removeImageListener(ImageListener il){
		listeners.remove(il);
	}
	
	@Override
	public void imageChanged(ImageEvent iE) {
	
		PlanarImage img = iE.getImage();
		
		KernelJAI kernel = new KernelJAI(11, 11, _matrix);
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(img);
    	pb.add(kernel);
    	m_image = JAI.create("dilate", pb);
		
		notifyListeners();
	}
	
}
